package String;
/**
 * 演示String的常量池和不变对象特点
 */
import javafx.print.PrintSides;
public class StringDemo {
    public static void main(String[] args) {
        String s1 = "123abc";
        String s2 = "123abc";
        System.out.println("s1:"+s1);//s1=123abc
        System.out.println("s2:"+s2);//s2=123abc

        System.out.println(s1 == s2);//ture
        String s3 = "123abc";
        System.out.println("s3:"+s3);//s3=123abc
        System.out.println(s1==s3);//ture

        String s4 = new String("123abc");//123abc
        System.out.println("s4:"+s4);//s4=123abc
        System.out.println(s1==s4);//false

        String s5 = "123"+"abc";
        System.out.println("s5"+s5);
        System.out.println(s1==s5);//ture

        String s = "123";
        String s6 =s+"abc";//拼接产生新的对象并且不会入常量池
        System.out.println("s6:"+s6);//s6:123abc
        System.out.println(s1==s6);//false

        /**
         * String 是不变对象：即：字符串对象创建后内容不可改变
         * 若改变则创建新的对象
         */

        s1 = s1+"!";//此时s1不再引用常量池中的对象，而是创建一个新对象
        System.out.println("s1:"+s2);//地址不再相同
        System.out.println(s1==s2);
        System.out.println("s2:"+s2);
        System.out.println("s3:"+s3);

    }
}
